// Running RD Regressions and Robustness
clear
eststo clear
// Using Ranking Data
use ../dta/index.dta, clear

//Merging on Returns and Industry Codes
merge 1:1 code rd_group using ../dta/daily_returns_rd.dta
gen stkcd=code
merge m:1 code using ../src/industry, nogen keep(match master)

// Generating Dummies
tab rd_group, gen(dum_rd_group)
tab ind, gen(dum_3ind)

// Benchmark RD First Stage (Table 3 - Panel A)
foreach var in D margin_hold60 margin_to_cap60 { // looping over dependent variables
	// Optimal Bandwidth
	rdrobust `var' running , covs(dum_rd_group* dum_3ind*) vce(cluster code) kernel(tri)
	eststo `var'_opt
	scalar n_total=e(N_h_l)+e(N_h_r)
	estadd scalar n_total
	
	// Fixed Bandwidth
	rdrobust `var' running , h(0.1) covs(dum_rd_group* dum_3ind*) vce(cluster code) kernel(tri)
	eststo `var'_fixed
	scalar n_total=e(N_h_l)+e(N_h_r)
	estadd scalar n_total
	
}

// Benchmark RD Reduced Form (Table 3 - Panel B)
foreach var in car_ret5 car_ret20 car_ret60 { // looping over dependent variables
	// Optimal Bandwidth
	rdrobust `var' running , covs(dum_rd_group* dum_3ind*) vce(cluster code) kernel(tri)
	eststo `var'_opt_rf
	scalar n_total=e(N_h_l)+e(N_h_r)
	estadd scalar n_total
	
	// Fixed Bandwidth
	rdrobust `var' running , h(0.1) covs(dum_rd_group* dum_3ind*) vce(cluster code) kernel(tri)
	eststo `var'_fixed_rf
	scalar n_total=e(N_h_l)+e(N_h_r)
	estadd scalar n_total
	
}

// Benchmark Fuzzy RD (Table 3 - Panel C)
foreach var in car_ret5 car_ret20 car_ret60 { // looping over dependent variables
	// Optimal Bandwidth
	rdrobust `var' running , covs(dum_rd_group* dum_3ind*) vce(cluster code) fuzzy(D) kernel(tri)
	eststo `var'_opt_fuz
	scalar n_total=e(N_h_l)+e(N_h_r)
	estadd scalar n_total
	
	// Fixed Bandwidth
	rdrobust `var' running , h(0.1) covs(dum_rd_group* dum_3ind*) vce(cluster code) fuzzy(D) kernel(tri)
	eststo `var'_fixed_fuz
	scalar n_total=e(N_h_l)+e(N_h_r)
	estadd scalar n_total
	
}


// Outputting Tables
esttab  D_opt margin_hold60_opt margin_to_cap60_opt D_fixed margin_hold60_fixed margin_to_cap60_fixed ///
	using ../esttab/tab3_a.tex, replace stats(pv_cl pv_rb h_l n_total, fmt(3 3 a3 a3) ///
	labels("P-Value" "CCT Robust P-Value" "Bandwidth" "N")  ///
	layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ) )	///
	b(3) se(3)  booktabs compress  alignment(D{.}{.}{-1}) keep(RD_Estimate) order(RD_Estimate) ///
	l  wrap  varwidth(30) frag  nomtitles nonumbers  noobs nonotes  star(* 0.10 ** 0.05 *** .01)	
	
esttab  car_ret5_opt_rf car_ret20_opt_rf car_ret60_opt_rf car_ret5_fixed_rf car_ret20_fixed_rf car_ret60_fixed_rf ///
	using ../esttab/tab3_b.tex, replace stats(pv_cl pv_rb h_l n_total, fmt(3 3 a3 a3) ///
	labels("P-Value" "CCT Robust P-Value" "Bandwidth" "N")  ///
	layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ) )	///
	b(3) se(3)  booktabs compress  alignment(D{.}{.}{-1}) keep(RD_Estimate) order(RD_Estimate) ///
	l  wrap  varwidth(30) frag  nomtitles nonumbers  noobs nonotes  star(* 0.10 ** 0.05 *** .01)	
	
esttab  car_ret5_opt_fuz car_ret20_opt_fuz car_ret60_opt_fuz car_ret5_fixed_fuz car_ret20_fixed_fuz car_ret60_fixed_fuz ///
	using ../esttab/tab3_c.tex, replace stats(pv_cl pv_rb h_l n_total, fmt(3 3 a3 a3) ///
	labels("P-Value" "CCT Robust P-Value" "Bandwidth" "N")  ///
	layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ) )	///
	b(3) se(3)  booktabs compress  alignment(D{.}{.}{-1}) keep(RD_Estimate) order(RD_Estimate) ///
	l  wrap  varwidth(30) frag  nomtitles nonumbers  noobs nonotes  star(* 0.10 ** 0.05 *** .01)		
	